package com.xlkj.tools;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

	public static final String dbName = "library";
	public static final int version = 1;
	public static final String tbName = "faces";
	public static final String _id = "id";
	public static final String _name = "name";
	public static final String _time = "time";
    public static final String _facefeature = "facefeature";

	public static final String _created = "created";

	public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + tbName + "(" + _id + " text not null," + _name + " text not null," + _time + " text not null," + _created + " INTEGER not null,"+_facefeature+" text not null )";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public Cursor select(String ia) {
		SQLiteDatabase db = getReadableDatabase();
		return db.query(tbName, null, _id + "=?", new String[] { ia },
				null, null, "created" + " asc");
	}

	public Cursor select() {
		SQLiteDatabase db = getReadableDatabase();
		return db.query(tbName, null, null, null, null, null, "created" + " desc");
	}

	public long insert(String id,String name,String time, String faceFeature, Long created) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(_id, id);
		cv.put(_name, name);
		cv.put(_time, time);
		cv.put(_facefeature, faceFeature);
		cv.put(_created, created);
		return db.insert(tbName, null, cv);
	}

	public long update(String id,String name,String time, String faceFeature, Long created) {
		SQLiteDatabase db = getWritableDatabase();
		ContentValues cv = new ContentValues();
        cv.put(_name, name);
		cv.put(_time, time);
        cv.put(_facefeature, faceFeature);
		cv.put(_created, created);
		return db.update(tbName, cv, _id + "=?", new String[] { id });
	}

	public int delete(String id) {
		SQLiteDatabase db = getWritableDatabase();
		return db.delete(tbName, _id + "=?", new String[] { id });
	}

	public int delete() {
		SQLiteDatabase db = getWritableDatabase();
		return db.delete(tbName, null, null);
	}

	public void deleAll(){
		SQLiteDatabase db = getWritableDatabase();
		String sql = "DELETE FROM " + tbName;
		db.execSQL(sql);
	}

}
